<?php
// +----------------------------------------------------------------------
// | WanhuCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://wanhunet.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 马宇翔 <eden_cola@aliyun.com>
// +----------------------------------------------------------------------

namespace app\index\taglib;

use think\template\TagLib;

/**
 * Wh标签库解析类
 * 参考Cx标签库解析类
 * @author    马宇翔 <eden_cola@aliyun.com>
 */
class Wh extends Taglib
{

    // 标签定义
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'test'        => ['attr' => 'value', 'close' => 0, 'expression' => true],
        'recuChild'   => ['attr' => '', 'close' => 0, 'expression' => true],
        'recursion'   => ['attr' => 'list, flag, key, item, child'],
        'form'        => ['attr' => 'id, type, class, enctype'],
        'search'      => ['attr' => 'id, class'],
        'page'        => ['attr' => 'count'],
        'title'       => ['attr' => '', 'close' => 0, 'expression' => true],
    ];

    /**
     * 测试标签
     */
    public function _test($tag, $content)
    {
        $value    = isset($tag['value']) ? $tag['value'] : 'default';
        return 'you say '.$value;
    }

    /**
     * 递归输出标签
     */
    public function _recursion($tag, $content)
    {
        $parseStr   = '';
        $flag       = isset($tag['flag'])   ? $tag['flag']  : 'recuChild';
        $list       = isset($tag['list'])   ? $tag['list']  : [];
        $key        = isset($tag['key'])    ? $tag['key']   : 'key';
        $item       = isset($tag['item'])   ? $tag['item']  : 'item';
        $child      = isset($tag['child'])  ? $tag['child'] : '';
        //$a = function($list, $a){
        //    if(empty($list))return false;
        //    foreach($list as $key => $item){
        //        $child = isset($item['child'])? $item['child']: [];
        //        if(!empty($item['child']))
        //            $a($item['child'], $a);
        //    }
        //};$a($arr, $a);
        $parseStr .= '<?php '; 
        $parseStr .= '$'.$flag.' = function($list, $recursionFunction){'; 
        $parseStr .= 'if(empty($list))return false;'; 
        $parseStr .= 'foreach($list as $'.$key.' => $'.$item.'){ '; 
        
        if(!empty($child)){
            $parseStr .= '$child = isset($'.$item.'[\''.$child.'\'])? $'.$item.'[\''.$child.'\']: [];'; 
        }

        $parseStr .= ' ?>'; 
        $parseStr .= $content; 
        $parseStr .= '<?php '; 
        $parseStr .= ' };};$'.$flag.'($'.$list.', $'.$flag.');'; 
        $parseStr .= ' ?>'; 
        return $parseStr;
    }
    
    /**
     * 递归子标签
     * 放在递归标签内才可以正常生效
     */
    public function _recuChild($tag, $content)
    {
        $parseStr  = '<?php '; 
        $parseStr .= 'if(!empty($child))'; 
        $parseStr .= '$recursionFunction($child, $recursionFunction);'; 
        $parseStr .= ' ?>'; 
        return $parseStr;
    }
    
    /**
     * 表单
     */
    public function _form($tag, $content)
    {
        $id        = isset($tag['id'])    ? $tag['id']   : '0';
        $enctype      = isset($tag['enctype'])  ? $tag['enctype'] : '';
        $class     = isset($tag['class']) ? $tag['class']: '';
        //<form method="post" action="$url">
        //    $content
        //</form>
        $parseStr  = '<?php $url = U(\'\', array_merge($_GET, [\'form\'=>'.$id.', \'type\'=>\'content\'])); ?>'; 
        $parseStr .= '<form method="post" enctype="'.$enctype.'" action="<?php echo $url; ?>" class="'.$class.'">'; 
        $parseStr .= $content; 
        $parseStr .= '</form>'; 
        return $parseStr;
    }
    
    /**
     * 搜索
     */
    public function _search($tag, $content)
    {
        $id        = isset($tag['id'])    ? $tag['id']   : '0';
        $class     = isset($tag['class']) ? $tag['class']: '';
        //<form method="post" action="$url">
        //    $content
        //</form>
        $parseStr  = '<?php $url = U(\'/search/'.$id.'\'); ?>'; 
        $parseStr .= '<form method="get" action="<?php echo $url; ?>" class="'.$class.'">'; 
        if(false!==strpos(C('base_url'), '?')){
            $parseStr .= '<input name="s" type="hidden" value="/search/'.$id.'">'; 
        }
        $parseStr .= $content; 
        $parseStr .= '</form>'; 
        return $parseStr;
    }
    
    /**
     * 分页
     */
    public function _page($tag, $content)
    {
        $count     = isset($tag['count'])  ? $tag['count'] : '0';
        //foreach(getObj('Page', $count) as $p){
        //    $content;
        //}
        $parseStr  = '<?php foreach(getObj(\'Page\', '.$count.') as $p){ ?>'; 
        $parseStr .= $content; 
        $parseStr .= '<?php }; ?>'; 
        return $parseStr;
    }
    
    /**
     * 标题
     * TODO
     */
    public function _title($tag, $content)
    {
        if(isset($content['title'])) echo $content['title'];
        if(isset($term['title'])) echo $term['title'];
        $parseStr  = '<?php  ?>'; 
        $parseStr .= $content; 
        $parseStr .= '<?php }; ?>'; 
        return $parseStr;
    }
}

